package com.proposition.user.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.proposition.commons.model.Permission;
import com.proposition.commons.model.Role;
import com.proposition.commons.model.RolePermission;
import com.proposition.commons.model.User;
import com.proposition.user.mapper.PermissionMapper;
import com.proposition.user.mapper.RoleMapper;
import com.proposition.user.mapper.RolePermissionMapper;
import com.proposition.user.mapper.UserMapper;
import com.proposition.user.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 * 角色接口实现类
 *
 * @author 杨凯
 * @create 2017-11-01 9:50
 **/
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

	@Autowired
	private RoleMapper roleMapper;

	@Autowired
	private RolePermissionMapper rolePermissionMapper;

	@Autowired
	private PermissionMapper permissionMapper;

	@Autowired
	private UserMapper userMapper;

	@Override
	public List<Role> findRoleList() {
		return roleMapper.selectList(new EntityWrapper<Role>());
	}

	@Override
	public boolean addRole(String title, Integer[] permissionId) {
		try {
			/*List<Permission> permissions = permissionMapper.selectList(new EntityWrapper<Permission>().in("id",permissionId));
			//将权限转为字符串
			String description = "";
			for (Permission permission : permissions){
				description+=permission.getName()+",";
			}
			//去除最后的逗号
			description = description.substring(0,description.length()-1);*/
			Role role = new Role();
			role.setName("member");
			role.setTitle(title);
			role.setDescription("拥有管理员设定的权限");
			roleMapper.addRole(role);
			rolePermissionMapper.insertBatchPermission(role.getId(),permissionId);
		} catch (Exception e) {
			e.printStackTrace();
			//手动回滚
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
		return true;
	}

	@Override
	public boolean deleteRole(Integer id) {
		//删除角色前首先判断角色下是否存在用户
		Wrapper<User> userWrapper = new EntityWrapper<>();
		userWrapper.where("u_role_id={0}",id).eq("u_status",0);
		Integer count = userMapper.selectCount(userWrapper);
		if(count != 0){
			//代表当前角色下存在用户
			return false;
		}
		Wrapper<RolePermission> rolePermissionWrapper = new EntityWrapper<>();
		rolePermissionWrapper.where("r_role_id={0}",id);
		rolePermissionMapper.delete(rolePermissionWrapper);
		roleMapper.deleteById(id);
		//删除成功返回1002
		return true;
	}

	@Override
	public boolean updateRole(Integer roleId, Integer[] permissionId) {

		/*List<Permission> permissions = permissionMapper.selectList(new EntityWrapper<Permission>().in("id",permissionId));
		//将权限转为字符串
		String description = "";
		for (Permission permission : permissions){
			description+=permission.getName()+",";
		}
		//去除最后的逗号
		description = description.substring(0,description.length()-1);*/
		Role role = new Role();
		role.setId(roleId);
		role.setDescription("拥有管理员设定的权限");
		roleMapper.updateById(role);
		Wrapper<RolePermission> rolePermissionWrapper = new EntityWrapper<>();
		rolePermissionWrapper.where("r_role_id={0}",roleId);
		rolePermissionMapper.delete(rolePermissionWrapper);
		rolePermissionMapper.insertBatchPermission(role.getId(),permissionId);
		return true;
	}


}
